<template>
  <basic-dialog
    width="60%"
    title="选择临采商品"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
  >
    <BasicForm
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :tableHeight="480"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '@/components/BasicTable';
import basePage from '@/mixins/commodity/basePage';
import { getTableAction } from '@/utils/getBtnsRender';
import { queryTemporaryGoodsService } from '@/api/commodity/common';
import { getSearchParamBySearchType } from '@/utils/getUtils';
export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: { productName: '', skuNo: '' },
      formItemList: [
        { label: '商品名称', prop: 'productName', type: 'input' },
        { label: 'SKU编码', prop: 'skuNo', type: 'input', searchType: 'EQ' }
      ],
      tableInfo: {
        list: [],
        columns: [
          { title: '商品名称',  key: 'productName' },
          { title: '商品编码', key: 'skuNo' },
          { title: '商品规格',  key: 'specModel' },
          { title: '商品品牌',  key: 'brandName' },
          { title: '计量单位',  key: 'unitName' }
        ],
        loading: false
      }
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{
        label: '选择',
        method: this.handleSelect
      }];
      return getTableAction({ h, row, actionList })
    },
    // 获取表格数据
    async getTableData () {
      const searchParam = getSearchParamBySearchType(this.formItemList, this.formData);
      const data = { searchParam: JSON.stringify(searchParam), ...this.paginationParams };
      const loadingFn = state => { this.tableInfo.loading = state };
      const res = await queryTemporaryGoodsService(data, loadingFn);
      if (res?.retData) {
        this.tableInfo.list = res.retData?.results ?? [];
        this.pagination.totalNum = res.retData?.totalNum ?? 0;
      }
    },
    // 选择临采商品
    handleSelect (row) {
      this.$emit('getSelectResults', row);
      this.handleCloseDialog();
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
